package com.example.everying.framework.websocket.handler;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.everying.constant.ChatMessageConstant;
import com.example.everying.framework.websocket.service.WebSocketChatService;
import com.example.everying.model.entity.chat.ChatMessagesEntity;
import com.example.everying.model.entity.content.ActivityComment;
import com.example.everying.model.entity.user.UserEntity;
import com.example.everying.service.chat.ChatMessagesService;
import com.example.everying.service.content.ActivityCommentService;
import com.example.everying.service.user.UserService;
import jakarta.websocket.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CommentMessageHandler implements MessageHandler {


    private static ActivityCommentService activityCommentService;
    private static ChatMessagesService chatMessagesService;
    private static UserService userService;

    @Autowired
    private void setService(ActivityCommentService activityCommentService,
                            ChatMessagesService chatMessagesService,
                            UserService userService) {
        CommentMessageHandler.activityCommentService = activityCommentService;
        CommentMessageHandler.chatMessagesService = chatMessagesService;
        CommentMessageHandler.userService = userService;
    }

    /**
     * @param session
     * @param message
     */
    @Override
    public void handle(Session session, JSONObject message) {
        String userId = WebSocketChatService.getUserId(session);
        if (userId == null) {
            return;
        }

        int unreadCount = Math.toIntExact(chatMessagesService.count(new QueryWrapper<ChatMessagesEntity>().
                eq("messages_type", ChatMessageConstant.MESSAGE_TYPE_ACTIVITY_COMMENT).
                eq("to_user_id", userId).
                eq("msg_status", ChatMessageConstant.MESSAGE_STATUS_UNREAD)));
        // 获取最后一条评论
        ActivityComment comment = getLatestComment(userId);
        if (comment == null) {
            JSONObject resObj = new JSONObject();
            resObj.put("type", "commentMessage");
            resObj.put("unreadCount", unreadCount);
            WebSocketChatService.sendMessageToUserId(userId, resObj.toJSONString());
            return;
        }
        UserEntity parentUser = userService.getById(comment.getUserId());
        JSONObject resObj = new JSONObject();
        resObj.put("type", "commentMessage");
        resObj.put("user", parentUser);
        resObj.put("unreadCount", unreadCount);
        resObj.put("commentTime", comment.getCreateTime());
        WebSocketChatService.sendMessageToUserId(userId, resObj.toJSONString());

    }

    private ActivityComment getLatestComment(String userId) {
        ActivityComment replyComment = activityCommentService.getLastCommentHaveParent(userId);
        ActivityComment articleComment = activityCommentService.getLastCommentHaveArticle(userId);

        if (replyComment == null && articleComment == null) {
            return null;
        }
        if (replyComment == null) {
            return articleComment;
        }
        if (articleComment == null) {
            return replyComment;
        }

        return replyComment.getCreateTime().isAfter(articleComment.getCreateTime())
                ? replyComment
                : articleComment;
    }
}
